package org.zhangyp.springbootsecurity.service;

import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.zhangyp.springbootsecurity.mapper.TUserMapper;
import org.zhangyp.springbootsecurity.model.entity.TUser;

/**
 * 从数据库中查询用户信息
 *
 * @author ypzhang
 * @since 2025/6/20
 */
@Service
public class JdbcUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private TUserMapper tUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询自己数据库的用户信息
        TUser user = tUserMapper.selectByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }
        return User.builder().username(user.getUsername()).password(user.getPassword()).roles("USER").build();
    }
}
